package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.ParamsMapper;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.common.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/02 16:28
 * @Description TODO:
 */
@Service
@Transactional
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private ParamsMapper paramsMapper;

    /**
     * 查询规格组信息
     * @param id
     * @return
     */
    public List<SpecGroupDTO> findGroupByCid(Long id) {

        SpecGroup spec=new SpecGroup();
        spec.setCid(id);
        List<SpecGroup> select = specGroupMapper.select(spec);
        if (CollectionUtils.isEmpty(select)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        //对象转换
        return BeanHelper.copyWithCollection(select, SpecGroupDTO.class);
    }

    /**
     * 查询规格参数信息
     * @return
     */
    public List<SpecParamDTO> findParams(Long gid, Long cid, Boolean searching) {
        if (gid==null&&cid==null){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        SpecParam record=new SpecParam();
        record.setCid(cid);
        record.setGroupId(gid);
        record.setSearching(searching);
        List<SpecParam> select = paramsMapper.select(record);
        
       if (CollectionUtils.isEmpty(select)){
           throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
       }
        return BeanHelper.copyWithCollection(select, SpecParamDTO.class);

    }

    //根据categoryId查询规格参数组和组内参数
    public List<SpecGroupDTO> findGroupByCidWithParams(Long id) {
        //根据商品分类查询出分类下的规格组列表
        List<SpecGroupDTO> groups = findGroupByCid(id);
        //根据商品分类查询出分类下的规格参数列表
        List<SpecParamDTO> params = findParams(null, id, null);

        //第三种：使用流式编程对params根据groudId分组
        Map<Long, List<SpecParamDTO>> paramsMap = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //把对应的规格组下的规格参数赋值进去
        groups.forEach(group->{
            group.setParams(paramsMap.get(group.getId()));
        });

//        //第二种方法：参数遍历
//        groups.forEach(group->{
//            params.forEach(param->{
//                if (group.getId()==param.getGroupId()){
//                    group.getParams().add(param);
//                }
//            });
//        });

//        //遍历groups,第一种方法
//        groups.forEach(group->{
//            SpecParam record=new SpecParam();
//            record.setGroupId(group.getId());
//            List<SpecParam> specParams = paramsMapper.select(record);
//            group.setParams(BeanHelper.copyWithCollection(specParams,SpecParamDTO.class));
//        });

        return groups;
    }
}
